package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_ProjectMapper;
import com.bc.bcapi.dao.T_User_seller_infoMapper;
import com.bc.bcmodel.domain.T_ProjectModel;
import com.bc.bcmodel.domain.T_User_seller_infoModel;
import com.bc.bcmodel.req.FeedBackReq;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mchange.lang.IntegerUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_FeedbackMapper;
import com.bc.bcmodel.domain.T_FeedbackModel;

/**
 * @Name T_FeedbackService * @Description * @Author generation * @Date 2018/8/12 20:44:10 * @Version 1.0
 **/
@Service
public class T_FeedbackService {
    @Resource
    T_FeedbackMapper t_FeedbackMapper;
    @Resource
    T_User_seller_infoMapper t_user_seller_infoMapper;
    @Resource
    T_ProjectMapper t_projectMapper;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_FeedbackModel req) {
        return t_FeedbackMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_FeedbackModel> req) {
        return t_FeedbackMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_FeedbackModel findByNamedParam(T_FeedbackModel req) {
        return t_FeedbackMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_FeedbackModel> getAllList(T_FeedbackModel req) {
        return t_FeedbackMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_FeedbackModel> getList(T_FeedbackModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_FeedbackModel> list = t_FeedbackMapper.findByNamedParamList(req);
        return new PageInfo<T_FeedbackModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_FeedbackModel req) {
        return t_FeedbackMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_FeedbackMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_FeedbackModel req) {
        return t_FeedbackMapper.count(req);
    }

    /**
     * 用户反馈
     */
    public JsonResult feedBack(FeedBackReq req) {
        if (StringUtils.isEmpty(req.getType()) || StringUtils.isEmpty(req.getDesc())) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        req.setPic(StringUtils.isEmpty(req.getPic()) ? "" : req.getPic());
        JsonResult js = new JsonResult();
        T_FeedbackModel t_feedbackModel = new T_FeedbackModel();
        if (!StringUtils.isEmpty(req.getUser_id())) {
            //有用户
            //卖家
            int platform = req.getPlatform();
            if (platform == 1) {
                T_User_seller_infoModel t_user_seller_infoModelParam = new T_User_seller_infoModel();
                t_user_seller_infoModelParam.setUser_id(req.getUser_id());
                T_User_seller_infoModel t_user_seller_infoModel = t_user_seller_infoMapper.findByNamedParam(t_user_seller_infoModelParam);
                int user_auth_status = t_user_seller_infoModel == null ? 0 : IntegerUtils.parseInt(t_user_seller_infoModel.getStatus(), 0);
                t_feedbackModel.setUser_auth_status(user_auth_status);

                T_ProjectModel t_projectModelParam = new T_ProjectModel();
                t_projectModelParam.setUser_id(req.getUser_id());
                List<T_ProjectModel> list_project = t_projectMapper.findByNamedParamList(t_projectModelParam);
                if (list_project != null && list_project.size() > 0) {
                    T_ProjectModel t_projectModel = list_project.get(0);
                    t_feedbackModel.setProject_aut_status(t_projectModel.getStatus());
                    t_feedbackModel.setPid(t_projectModel.getPid());
                }
            } else if (platform == 2) {
                //买家
                t_feedbackModel.setUser_auth_status(1);
            }
        }
        t_feedbackModel.setQ_type(req.getType());
        t_feedbackModel.setEmail(req.getEmail());
        t_feedbackModel.setQ_desc(req.getDesc());
        t_feedbackModel.setPlatform(req.getPlatform());
        t_feedbackModel.setPic(req.getPic());
        t_FeedbackMapper.insert(t_feedbackModel);
        return js;
    }

}